var app = getApp();
var datas = require('../../utils/data');
Page({

  data: {
    dataid:-1,
    frame: null,
    itemList:[],
    current:0, 
    head:"以下问题，请选择符合或接近您孩子实际表现情况的答案。" 
  }, 
  onLoad: function (options) { 
    console.log(app.globalData.information);
    var that = this;
    var itemList=[];
    for(var i=0;i<(20+55+60);i++){
      itemList[i]=-1;
    } 
    // that.setData({ userInfo: app.globalData.userInfo }); 
    that.setData({ frame: datas.education,itemList : itemList });

  },
  formation(){
    var that =this; 
    var sumitItemList=[];
    var orginitemList =that.data.itemList;
    var itemList20=[];
    var itemList55 = [];
    var itemList60 = [];

    var itemList =[];

    for (var i = 115; i < orginitemList.length; i++) {
      itemList20[i-115] = orginitemList[i];
    }

    for (var i = 60; i < 55+60; i++) {
      itemList55[i-60] = orginitemList[i];
    }

    for (var i = 0; i < 60; i++) {
      itemList60[i] = orginitemList[i];
    }

    itemList.concat(itemList20);
    itemList.concat(itemList55);
    itemList.concat(itemList60);
     
    for (var i = 0; i < itemList.length ;i++){
      var item = { id: "", selid: "", flag: "" };
      if(i < 20){
        item.id = i + 1;
        item.selid = itemList[i];
        item.flag = datas.interpersonal[i].flag;
      } else if ( i < 75){
        item.id = i - 19;
        item.selid = itemList[i];
        item.flag = datas.action[i - 20].flag;
      }else {
        item.id = i - 74;
        item.selid = itemList[i];
        item.flag = datas.education[i - 75].flag;
      } 
      sumitItemList.push(item);
    }

    var content = { "information": app.globalData.information, "answer": sumitItemList};
    console.log(content);
    app.postrequest("user/add", { user_id: 1, content: JSON.stringify(content) },function(data,json){

    });
  },
  submit(){
    var that =this;
    that.formation();
    wx.showModal({
      title: '提交成功',
      content: '请等待后台分析问卷',
      confirmText: "前往首页",
      cancelText: "再看看题",
      success: function (res) {
        console.log(res);
        if (res.confirm) {
          console.log('前往首页') 
          wx.switchTab({
            url: '/page/index/index',
          }) 
        } else {
          console.log('再看看题')
        }
      }
    });
  },
  juge(){
    var that=this;
    if (that.data.frame.length == 20 ){
        return this.data.current;
    } else if (that.data.frame.length == 55){
        return this.data.current+20;
    } else if (that.data.frame.length == 60) {
        return this.data.current+75;
    }
  },
  selecttap(e){
    var that = this; 
    var itemList=that.data.itemList;
    var actualcurrent=that.juge();

    itemList[actualcurrent] = e.currentTarget.dataset.id;
    console.log(e);
    console.log(e.currentTarget.dataset.id);
    // console.log(e._relatedInfo.anchorTargetText);
    that.setData({ dataid: e.currentTarget.dataset.id, itemList: itemList})
  },
  init(){
    var that = this; 
    var itemList = that.data.itemList;
    that.setData({ dataid: itemList[that.juge()] });
  },
  next(){
    var that = this; 
    
    var frame=that.data.frame;
    var current = that.data.current; 
    if (that.data.dataid==-1){
        app.info("请选择选项");
    }else{
      // 设置目前也索引 current 以及标题
      if (current + 1 == frame.length) {
        that.setData({ current: 0 })
        if (frame.length == 60) {
          that.setData({ frame: datas.interpersonal, head: "以下问题，请选择符合或接近父母双方或任何一方对孩子的教养方式的答案。" })
          wx.showModal({
            content: '恭喜你完成儿童行为问卷了，开始家庭教养方式问卷作答吧',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              }
            }
          });
          wx.setNavigationBarTitle({
            title: '家庭教养方式问卷'
          })
        }
        if (frame.length == 55) {
          that.setData({ frame: datas.education, head: "以下问题，请选择符合或接近您孩子实际表现情况的答案。" })
          wx.showModal({
            content: '恭喜你完成了家庭教养方式问卷，开始家庭人际调查问卷作答吧',
            showCancel: false,
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              }
            }
          });
          wx.setNavigationBarTitle({
            title: '家庭人际调查问卷'
          })
        }
      } else {
        that.setData({ current: ++current })
      }

      that.init();
    }

  },
  pre() {
    var that = this;
    this.setData({
      current: --that.data.current
    })
    that.init();
  },
  open: function () {
    var that=this;
    var itemList = ['88元/5分钟', '100元/10分钟', '200元/20分钟']
    wx.showActionSheet({
      itemList: itemList,
      success: function (res) {
        if (!res.cancel) {
          that.setData({ time: itemList[res.tapIndex] })  
        }
      }
    });
  },
  setValue(e){ 
    this.setData({ wx_id:e.detail.value })
  },
  cancel(){ 
    this.setData({  frame: false,wx_id:"" })
  },
  confirm() {
    this.setData({ frame: false  })
  },
  frame() {
    this.setData({  frame: true })
  },
  
  preview:function(event){
     
  },
   
  formSubmit: function (event) {
    var that = this;
    var pid = event.currentTarget.dataset.pid;
    if (!app.globalData.userInfo) {
      app.error('请登录后再评论');
      return;
    }
    if (event.detail.value.message == '') {
      app.error('内容不能为空');
      return;
    }
    app.request('/comment/post', { aid: this.data.archivesInfo.id, pid: this.data.form.quotepid, username: event.detail.value.username, content: event.detail.value.content }, function (data) {
      app.success('发表成功!');
      that.setData({ form: { quotepid: 0, message: '', focus: false }, commentList: [], nodata: false, nomore: false });
      if (that.data.commentList.length < 10) {
        that.page = 1;
      } else {
        that.data.commentList = that.data.commentList.slice(0, 10);
        that.page = 2;
      }
      that.onReachBottom();
    }, function (data, ret) {
      app.error(ret.msg);
    });
  },
  vote: function (event) {
    var that = this;
    app.vote(event, function (data) {
      that.setData({ ['archivesInfo.likes']: data.likes, ['archivesInfo.dislikes']: data.dislikes, ['archivesInfo.likeratio']: data.likeratio });
    });
  },
  onReachBottom: function () {
    var that = this;
    this.loadComment(function (data) {
      if (data.commentList.length == 0) {
        //app.info("暂无更多数据");
      }
    });
  },
  loadComment: function (cb) {
    var that = this;
    if (that.data.nomore == true || that.data.loading == true) {
      return;
    }
    this.setData({ loading: true });
    app.request('/comment', { aid: this.data.archivesInfo.id, page: this.page }, function (data, ret) {
      data.commentList.forEach(function (item) {
        item.article = app.towxml.toJson(item.content, 'html');
      });
      that.setData({
        loading: false,
        nodata: that.page == 1 && data.commentList.length == 0 ? true : false,
        nomore: that.page > 1 && data.commentList.length == 0 ? true : false,
        commentList: that.page > 1 ? that.data.commentList.concat(data.commentList) : data.commentList,
      });
      that.page++;
      typeof cb == 'function' && cb(data);
    }, function (data, ret) {
      that.setData({
        loading: false
      });
      app.error(ret.msg);
    });
  },

  share: function () {
    wx.showShareMenu({});
  },
  onShareAppMessage: function () {
    return {
      title: this.data.archivesInfo.title,
      desc: this.data.archivesInfo.intro,
      path: '/page/product/detail?id=' + this.data.archivesInfo.id
    }
  },
})